package game.module.nn.processor;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.hall.logic.CheatManager;
import game.module.nn.bean.NnRoom;
import game.module.nn.logic.NnDataManager;
import game.module.nn.logic.NnLogic;
import game.module.offline.bean.PlayerOfflineCache;
import game.module.offline.logic.PlayerOfflineManager;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageNn.C2SNNJoinRoom;
import lua.LuaMessageNn.NNMemberInfo;
import lua.LuaMessageNn.PushNNPlayerJoin;
import lua.LuaMessageNn.S2CNNJoinRoom;

@MsgCodeAnn(msgcode = C2SNNJoinRoom.id, accessLimit = 500)
public class NnJoinRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(NnJoinRoomProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SNNJoinRoom reqMsg = C2SNNJoinRoom.parse(request);
		String room_id = reqMsg.room_id;
		int playerId = hero.getId();
		logger.info("nn join room,playeyId={},room_id={}", playerId, room_id);
		// 是否在房间中
		boolean isInRoom = NnDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is fang not exist
		if (!NnDataManager.getInstance().checkRoomExist(room_id)) {
			S2ErrorCode respMsg = new S2ErrorCode(104);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		final NnRoom nnRoom = NnDataManager.getInstance().getNnRoom(room_id);

		int fangka = hero.getPlayerBean().getFang_ka();
		int fangka_cost = nnRoom.getRoomfee();
		if (nnRoom.getRoomFeeType() == 2 && fangka < fangka_cost)// 赢家收费
		{
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (nnRoom.getRoomFeeType() == 1 && fangka < fangka_cost / 2) {// AA
			S2ErrorCode respMsg = new S2ErrorCode(103);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		if (nnRoom.getIs_jindou() == 1 && hero.getPlayerBean().getJin_dou() < nnRoom.getJindou() * nnRoom.getRoundCount() * 5) {// 金豆房间
			S2ErrorCode respMsg = new S2ErrorCode(133);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// 人数已满
		if (nnRoom.getPlayerList().size() >= 5) {
			S2ErrorCode respMsg = new S2ErrorCode(105);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
        
		if (CheatManager.getInstance().checkCheatAndJoin(nnRoom.getPrevent_cheat(), nnRoom.getIps(), hero)) {
			return;
		}

		NnLogic.getInstance().addRoomPlayer(playerId, room_id);
		// ret
		S2CNNJoinRoom respMsg = new S2CNNJoinRoom();
		respMsg.prevent_cheat = nnRoom.getPrevent_cheat();
		respMsg.room_id = room_id;
		respMsg.round_count = nnRoom.getRoundCount();
		respMsg.roomFeeType = nnRoom.getRoomFeeType();
		respMsg.members = new NNMemberInfo[nnRoom.getPlayerList().size()];
		List<Integer> playerIdList = nnRoom.getPlayerList();
		int i = 0;
		for (Integer aPlayerId : playerIdList) {
			PlayerOfflineCache poc = PlayerOfflineManager.getInstance().getPlayerOfflineCache(aPlayerId);
			respMsg.members[i++] = new NNMemberInfo(aPlayerId, poc.getHeadimg(), poc.getLevel(), poc.getSex().shortValue(), poc.getName());
		}
		hero.writeAndFlush(respMsg.build(hero.alloc()));
		// data
		NnDataManager.getInstance().addPlayer(playerId, room_id);
		// push
		PushNNPlayerJoin pushPlayerJoin = new PushNNPlayerJoin();
		pushPlayerJoin.seat_index = (short) (nnRoom.getPlayerList().size() - 1);
		pushPlayerJoin.member_info = new NNMemberInfo(playerId, hero.getPlayerBean().getHeadimg(), hero.getPlayerBean().getLevel(), hero.getPlayerBean().getSex().shortValue(),
				hero.getPlayerBean().getName());
		for (Integer aPlayerId : playerIdList) {
			if (aPlayerId == playerId) {
				continue;
			}
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushPlayerJoin.build(pr.alloc()));
			}
		}
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

}
